package com.siha_back.controller;

import com.siha_back.client.DeepSeekClient;
import com.siha_back.common.Result;
import com.siha_back.entity.dto.MockInterview;
import com.siha_back.service.MockInterviewService;
import com.siha_back.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;

@RestController
@RequestMapping("/dialogue")
public class DialogueController {

    @Autowired
    private MockInterviewService mockInterviewService;

    /**
     * 通过岗位名称，用户的回答，获取下一个问题，更新面试记录信息
     *
     * @param  jobname
     * @return 实例对象
     */
    @PostMapping("/ask")
    public Result add(@RequestParam("jobname")String jobname, @RequestBody(required = false) String question) {
        //获取当前会话id
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getSession().getId();
        System.out.println(question);
        if(question == null){
            question = "我面试的岗位是"+jobname+"请你作为一个面试官对我提出一个问题，不做任何提示,回答时请你只输出问题";
        } else if (question.contains("结束面试")){
            question = "面试结束，请你对我进行评价,回答时只输出评价";
            String ask1 = DeepSeekClient.ask(question,sessionId);
            System.out.println(ask1);

            question = "请你对我的专业知识,进行评分，满分100，不小于20分，回答时只输出分数";
            String ask2 = DeepSeekClient.ask(question,sessionId);
            int score = Integer.parseInt(ask2);
            System.out.println(ask2);

            question = "请你对我的专业技能,进行评分，满分100，不小于20分，回答时只输出分数";
            String ask3 = DeepSeekClient.ask(question,sessionId);
            int score1 = Integer.parseInt(ask3);
            System.out.println(ask3);

            question = "请你对我的工作经验,进行评分，满分100，不小于20分，回答时只输出分数";
            String ask4 = DeepSeekClient.ask(question,sessionId);
            int score2 = Integer.parseInt(ask4);
            System.out.println(ask4);

            question = "请你对我的个性特征,进行评分，满分100，不小于20分，回答时只输出分数";
            String ask5 = DeepSeekClient.ask(question,sessionId);
            int score3 = Integer.parseInt(ask5);
            System.out.println(ask5);

            question = "请你对我的求职动机,进行评分，满分100，不小于20分，回答时只输出分数";
            String ask6 = DeepSeekClient.ask(question,sessionId);
            int score4 = Integer.parseInt(ask6);
            System.out.println(ask6);

            question = "请你对我的价值观,进行评分，满分100，不小于50分，回答时只输出分数";
            String ask7 = DeepSeekClient.ask(question,sessionId);
            int score5 = Integer.parseInt(ask7);
            System.out.println(ask7);


            Map<String, Object> map = ThreadLocalUtil.get();
            Integer id = (Integer) map.get("id");

            //犹豫次数
            int count = 5;

            //将上述所有信息存入面试记录信息
            MockInterview mockInterview = new MockInterview(null,id,"deepseek",score,score1,score2,score3,score4,score5,count,ask1);
            mockInterviewService.insert(mockInterview);

            return Result.success(200,"success",ask1);
        }else{
            question = "我面试的岗位是"+jobname+"上一个问题的回答是"+question+"请你作为一个面试官对我提出下一个问题，不做任何提示,回答时请你只输出问题";
            System.out.println(question);
        }
        String ask = DeepSeekClient.ask(question,sessionId);
        System.out.println(ask);
        return Result.success(200,"success",ask);
    }
}
